package DAL;

import Entity.BCategory;
import Entity.Books;
import Entity.Borrow;
import IDAL.BookBorrowingInterface;
import Utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.util.List;

/**
 * @Author Chen
 * @Date 2022/1/7 11:06
 * @Version 1.0
 */
public class BorrowDao implements BookBorrowingInterface {
    @Override
    public int addBorrowInfo(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "insert into l_borrow(bookNumber,readersNumber,hoursOfLoanService)values(?,?,?)";

            result = runner.update(conn, sql, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public int deleteBorrowInfo(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "delete from l_borrow where bookNumber=?";
            result = runner.update(conn, sql, args);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public int updateBorrowInfo(Object... args) {
        int result = 0;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "update l_borrow set " +
                    " readersNumber = ?," +
                    " hoursOfLoanService = ?," +
                    " returnTime = ?," +
                    " fine = ?" +
                    " where bookNumber = ?";
            result = runner.update(conn, sql, args);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public List<Borrow> selectAllBorrowInfo() {
        List<Borrow> result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_borrow";
            BeanListHandler<Borrow> handler = new BeanListHandler<>(Borrow.class);
            result = runner.query(conn, sql, handler);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public List<Borrow> selectBorrowInfoByReaderId(Object... args) {
        List<Borrow> result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_borrow where readersNumber = ?";
            BeanHandler<Borrow> handler = new BeanHandler<>(Borrow.class);
            result = (List<Borrow>) runner.query(conn, sql, handler, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }

    @Override
    public Borrow selectBorrowInfoByBookId(Object... args) {
        Borrow result = null;
        Connection conn = null;
        try {
            QueryRunner runner = new QueryRunner();
            conn = JDBCUtils.getConnection3();
            String sql = "select * from l_borrow where bookNumber = ?";
            BeanHandler<Borrow> handler = new BeanHandler<>(Borrow.class);
            result = runner.query(conn, sql, handler, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return result;
    }
}
